from frame.config import Config

from flask import Flask, render_template, request, session, g

from web import product_blueprint, user_blueprint

app = Flask(__name__, template_folder="web/templates", static_folder="web/static")
app.register_blueprint(product_blueprint)
app.register_blueprint(user_blueprint)
app.config.from_object(Config)

IGNORE_PREFIX = ['/static', '/user/login']


@app.before_request
def before_request():
    p = request.path
    g.msg = {"status": "error", "message": ""}
    for ignore_prefix in IGNORE_PREFIX:
        if p.startswith(ignore_prefix):
            return
    if 'user' in session and session['user']:
        g.user = session['user']
    else:
        return render_template('user/login.html')


@app.route("/")
def alive():
    return render_template('index.html')


@app.errorhandler(404)
def error_404(error):
    return render_template('404.html', error=error)


@app.errorhandler(500)
@app.errorhandler(Exception)
def error_500(error):
    return render_template('500.html', error=error)


if __name__ == '__main__':
    app.run(app.config.get('HOST'), app.config.get('PORT'), app.config.get('DEBUG'), threaded=True)


    # python -m pwiz -e mysql -H 43.254.44.139 -p3306 -u root -P  crane_machine> web/model/crane_machine.py
